<?php
namespace app\zftpay\controller;
use app\BaseController;
use DateTime;
use think\facade\Db;

ini_set('max_execution_time','0');

class Task extends BaseController{

    public function doNotify(){
        $data=Db::table("zft_order_success")
            ->where("notify_time",null)
            //->where("uid","<","120277")
            ->select();
        foreach($data as $info){
            echo $info["uid"];echo "<br/>";
            $queueName  	  = "app\job\ZftNotifyUrl";
            $jobData       	  = [ 'order_uid' =>$info["uid"],"alipay_notify_con"=>$info["alipay_notify_con"],"community_id"=>$info["community_id"]] ;
            $resu= \think\facade\Queue::push($queueName,$jobData);
        }
        //var_dump($data);
    }
    public function test2(){
        $where=array();
        $where[] = ['id','<',908];
        $data=Db::table("zft_order_success")
            ->where('uid','<',908)
            ->select();
//        var_dump($data);
        Db::startTrans();
        try {
            foreach($data as $info){
                Db::table("zft_order_success")->where("uid",$info["uid"])->delete();
                $info["del_time"]=date("Y-m-d H:i:s");
                Db::table("zft_order_success_del")->insert($info);
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            var_dump($e);
            // 回滚事务
            Db::rollback();
        }
        echo "success";
    }
    public function testDay(){

        // 获取当前日期
        $nowDate=date("Y-m-d");

        // 遍历过去30天
        for ($i = 58; $i > 0; $i--) {
            $start=date("Y-m-d",strtotime("-".$i." day"));
            $end=date("Y-m-d",strtotime("-".($i-1)." day"));
            $where=array();
            $where[] = ['createTime','>=',strtotime($start)];
            $where[] = ["createTime","<",strtotime($end)];
            $order_money=Db::table("zft_settle_order")
                ->where($where)

                ->sum("order_money");
            $settle_money=Db::table("zft_settle_order")
                ->where($where)

                ->sum("settle_money");
            $add["order_money"]=$order_money;
            $add["settle_money"]=$settle_money;
            $add["day"]=$start;
            $add["create_time"]=date("Y-m-d H:i:s");
            Db::table("zft_statistics")->insert($add);
            echo $start."----".$end."---".$order_money."--".$settle_money;echo "</br>";
            //echo $start."----".$money;echo "</br>";
        }

    }

    public function statis(){
        $start=date("Y-m-d",strtotime("-1 day"));
        $end=date("Y-m-d");
        $where=array();
        $where[] = ['createTime','>=',strtotime($start)];
        $where[] = ["createTime","<",strtotime($end)];
        $order_money=Db::table("zft_settle_order")
            ->where($where)

            ->sum("order_money");
        $settle_money=Db::table("zft_settle_order")
            ->where($where)

            ->sum("settle_money");
        $add["order_money"]=$order_money;
        $add["settle_money"]=$settle_money;
        $add["day"]=$start;
        $add["create_time"]=date("Y-m-d H:i:s");
        Db::table("zft_statistics")->insert($add);
    }

    public function test(){
        $start=date("Y-m-d",strtotime("-60 day"));
//echo 2;exit;
        $nowDate=date("Y-m-d");
        $where=array();
        $where[] = ['create_time','<=',$start];
        $where[] = ["settle_status","=","1"];
        $data=Db::table("zft_order_success")
            ->where($where)
            ->limit(10000)
            ->count();
        var_dump($data);
    }
    //收款统计
    public function delOrder(){

        $start=date("Y-m-d",strtotime("-5 day"));
//echo 2;exit;
        $where=array();
        $where[] = ['create_time','<=',$start];
        $where[] = ["settle_status","=","1"];
        $data=Db::table("zft_order_success")
            ->where($where)
            ->limit(20000)
            ->select();
        Db::startTrans();
        try {
            foreach($data as $info){
                Db::table("zft_order_success")->where("uid",$info["uid"])->delete();
                $info["del_time"]=date("Y-m-d H:i:s");
                Db::table("zft_order_success_del")->insert($info);

                $order=Db::table("zft_settle_order")->where("order_uid",$info["uid"])->find();
                if(!empty($order)){
                    $order["del_time"]=date("Y-m-d H:i:s");
                    Db::table("zft_settle_order_del")->insert($order);
                    Db::table("zft_settle_order")->where("id",$order["id"])->delete();
                }
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            var_dump($e);
            // 回滚事务
            Db::rollback();
        }
        echo "删除成功";
        //var_dump($data);
    }

    public function delSettleOrder(){
        $data=Db::table("zft_order_success_del")
            ->limit(70000,10000)
            ->select();
        Db::startTrans();
        try {
            foreach($data as $info){
                $order=Db::table("zft_settle_order")->where("order_uid",$info["uid"])->find();
                if(!empty($order)){
                    $order["del_time"]=date("Y-m-d H:i:s");
                    Db::table("zft_settle_order_del")->insert($order);
                    Db::table("zft_settle_order")->where("id",$order["id"])->delete();
                }

            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            var_dump($e);
            // 回滚事务
            Db::rollback();
        }
        echo "删除成功";
    }

}